<?php
include('../../includes/classes/core.php');
$start  	= $_REQUEST['start'];
$end    	= $_REQUEST['end'];
$count 		= $_REQUEST["count"];
$action 	= $_REQUEST['act'];
$departament= $_REQUEST['departament'];
$type       = $_REQUEST['type'];
$category   = $_REQUEST['category'];
$s_category = $_REQUEST['sub_category'];
$done 		= $_REQUEST['done']%3;
$name 		= $_REQUEST['name'];
$user 		= $_REQUEST['user'];
$title 		= $_REQUEST['title'];
$text[0] 	= "დავალებები პასუხისმგებელი პირების მიხედვით";
$text[1] 	= "'$type'- სტატუსების მიხედვით";
$text[2] 	= "'$type'-'$category'-კატეგორიების მიხედვით";

$start = $db->convertDateTime($start);
$end = $db->convertDateTime($end);

if ($category=="გადაცემულია გასარკვევად") $c=0;
elseif ($category=="გარკვევის პროცესშია") $c=1;
elseif ($category=="მოგვარებულია, აცნობეთ") $c=2;
elseif ($category=="დასრულებული") $c=3;

//------------------------------------------------query-------------------------------------------
switch ($done){
	case  1:
		if ($user==0){
		$result = mysql_query("	SELECT `status`.`call_status` AS category,
											COUNT(*),
											CONCAT(ROUND(COUNT(*)/(SELECT COUNT(*)
												FROM 	task
												LEFT JOIN crystal_users ON task.responsible_user_id=crystal_users.id
												LEFT JOIN incomming_call ON incomming_call.id=task.incomming_call_id
												LEFT JOIN `status` ON task.`status`=`status`.id
												
												WHERE (crystal_users.`name`='$type') and `task`.`date` >= '$start' and `task`.`date` <= '$end'
												)*100,2),'%') AS percent
									
									FROM 	task
												LEFT JOIN crystal_users ON task.responsible_user_id=crystal_users.id
												LEFT JOIN incomming_call ON incomming_call.id=task.incomming_call_id
												LEFT JOIN `status` ON task.`status`=`status`.id
												
												WHERE (crystal_users.`name`='$type') and `task`.`date` >= '$start' and  `task`.`date` <= '$end'
									GROUP BY category");
		}else 
		{
			$result = mysql_query("	SELECT `status`.`call_status` AS category,
					COUNT(*),
					CONCAT(ROUND(COUNT(*)/(SELECT COUNT(*)
					FROM 	task
					LEFT JOIN crystal_users ON task.responsible_user_id=crystal_users.id
					LEFT JOIN incomming_call ON incomming_call.id=task.incomming_call_id
					LEFT JOIN `status` ON task.`status`=`status`.id
			
					WHERE (crystal_users.`name`='$type') and `task`.`date` >= '$start' and  `task`.`date` <= '$end' AND task.responsible_user_id=$user
			)*100,2),'%') AS percent
						
					FROM 	task
					LEFT JOIN crystal_users ON task.responsible_user_id=crystal_users.id
					LEFT JOIN incomming_call ON incomming_call.id=task.incomming_call_id
					LEFT JOIN `status` ON task.`status`=`status`.id
			
					WHERE (crystal_users.`name`='$type') and `task`.`date` >= '$start' and  `task`.`date` <= '$end' AND task.responsible_user_id=$user
					GROUP BY category");
		}
		$text[0]=$text[1];
	break;
	case 2:
		if ($user==0){
			$result = mysql_query("SELECT if(ISNULL(task.incomming_call_id), category.`name`, cat.`name` ) AS category,
											COUNT(*),
											CONCAT(ROUND(COUNT(*)/(SELECT COUNT(*)
												FROM 	task
												JOIN crystal_users ON task.responsible_user_id=crystal_users.id
												LEFT JOIN incomming_call ON incomming_call.id=task.incomming_call_id
												LEFT JOIN category AS cat ON incomming_call.call_subcategory_id=cat.id
												LEFT JOIN category ON task.category_id=category.id
												WHERE (task.`status`=$c)AND (crystal_users.`name`='$type') and `task`.`date` >= '$start' and `task`.`date` <= '$end'
												AND if(ISNULL(task.incomming_call_id), category.`name`, cat.`name` )!=''
												)*100,2),'%') AS percent
									FROM 	task
									JOIN crystal_users ON task.responsible_user_id=crystal_users.id
									LEFT JOIN incomming_call ON incomming_call.id=task.incomming_call_id
									LEFT JOIN category AS cat ON incomming_call.call_subcategory_id=cat.id
									LEFT JOIN category ON task.category_id=category.id
									WHERE (task.`status`=$c)AND (crystal_users.`name`='$type')  and `task`.`date` >= '$start' and  `task`.`date` <= '$end'
									AND if(ISNULL(task.incomming_call_id), category.`name`, cat.`name` )!=''
									GROUP BY category");
		}else 
		{
			$result = mysql_query("SELECT if(ISNULL(task.incomming_call_id), category.`name`, cat.`name` ) AS category,
					COUNT(*),
					CONCAT(ROUND(COUNT(*)/(SELECT COUNT(*)
					FROM 	task
					JOIN crystal_users ON task.responsible_user_id=crystal_users.id
					LEFT JOIN incomming_call ON incomming_call.id=task.incomming_call_id
					LEFT JOIN category AS cat ON incomming_call.call_subcategory_id=cat.id
					LEFT JOIN category ON task.category_id=category.id
					WHERE (task.`status`=$c)AND (crystal_users.`name`='$type') and `task`.`date` >= '$start' and  `task`.`date` <= '$end'
					AND if(ISNULL(task.incomming_call_id), category.`name`, cat.`name` )!='' AND task.responsible_user_id=$user
			)*100,2),'%') AS percent
					FROM 	task
					JOIN crystal_users ON task.responsible_user_id=crystal_users.id
					LEFT JOIN incomming_call ON incomming_call.id=task.incomming_call_id
					LEFT JOIN category AS cat ON incomming_call.call_subcategory_id=cat.id
					LEFT JOIN category ON task.category_id=category.id
					WHERE (task.`status`=$c)AND (crystal_users.`name`='$type')  and `task`.`date` >= '$start' and  `task`.`date` <= '$end'
					AND if(ISNULL(task.incomming_call_id), category.`name`, cat.`name` )!='' AND task.responsible_user_id=$user
					GROUP BY category");
		}
		$text[0]=$text[2];
	break;
	default:
		if ($user==0){
		$result = mysql_query("SELECT crystal_users.`name` as type,
										COUNT(*),
										CONCAT(ROUND(COUNT(*)/(SELECT COUNT(*) FROM task
										JOIN crystal_users ON task.responsible_user_id=crystal_users.id
										WHERE `task`.`date` >= '$start' AND `task`.`date` <= '$end')*100,2),'%')
								FROM 	task
								JOIN crystal_users ON task.responsible_user_id=crystal_users.id
								WHERE `task`.`date` >= '$start' AND `task`.`date` <= '$end'
								GROUP BY 	type;");

		break;
		}else
		{
			$result = mysql_query("SELECT crystal_users.`name` as type,
										COUNT(*),
										CONCAT(ROUND(COUNT(*)/(SELECT COUNT(*) FROM task
										JOIN crystal_users ON task.responsible_user_id=crystal_users.id
										WHERE `task`.`date` >= '$start' AND `task`.`date` <= '$end')*100,2),'%')
								FROM 	task
								JOIN crystal_users ON task.responsible_user_id=crystal_users.id
								WHERE `task`.`date` >= '$start' AND `task`.`date` <= '$end' AND task.responsible_user_id=$user
								GROUP BY 	type;");
		}
}
///----------------------------------------------act------------------------------------------
switch ($action) {
	case "get_list":
		$data = array("aaData"	=> array());
		while ( $aRow = mysql_fetch_array( $result ) )
		{	$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				$row[0] = '0';

				$row[$i+1] = $aRow[$i];
			}
			$data['aaData'][] =$row;
		}
		echo json_encode($data); return 0;
		break;
	case 'get_category' :
		$rows = array();
		while($r = mysql_fetch_array($result)) {
			$row[0] = $r[0];
			$row[1] = (float) $r[1];
			$rows['data'][]=$row;
		}
		$rows['text']=$text[0];
		echo json_encode($rows);
		break;
	case 'get_in_page':
		mysql_query("SET @i = 0;");
	  	$rResult = mysql_query("SELECT	    `task`.id,
										`task`.id,
										if(ISNULL(task.incomming_call_id), DATE_FORMAT(task.`date`,'%d/%m/%y %H:%i:%s'), DATE_FORMAT(incomming_call.`date`,'%d/%m/%y %H:%i:%s')) AS datee,
										if(ISNULL(task.incomming_call_id), task.`phone`, incomming_call.`phone`) AS datee,
										incomming_call.pin,
										incomming_call.pid,
										if(ISNULL(task.incomming_call_id), source.`name`, sou.`name` ) AS source,
										if(ISNULL(task.incomming_call_id), category.`name`, cat.`name` ) AS category,
										if(ISNULL(task.incomming_call_id), object.`name`, obj.`name` ) AS object,
										`crystal_users`.`name` ,
    									`crystal_users1`.`name` ,
										`status`.`call_status`,
										TIME_FORMAT(SEC_TO_TIME(asterisk_incomming.duration), '%i:%s')AS `duration`,
										CONCAT('<button class=\'download\' str=',asterisk_incomming.file_name,'>მოსმენა</button>') AS `file_name`
                            FROM 					`task`
                            LEFT JOIN 	incomming_call ON task.incomming_call_id=incomming_call.id
                           	LEFT JOIN 	`category` 		ON task.category_id=category.id
                            LEFT JOIN 	`category` AS cat 		ON incomming_call.call_subcategory_id=cat.id
                            LEFT JOIN 	`object` 		ON task.object_id=object.id
                            LEFT JOIN 	`object` AS obj 		ON incomming_call.object_id=obj.id
                            LEFT JOIN 	source	AS sou	ON incomming_call.source_id=sou.id
                            LEFT JOIN 	source ON task.task_source_id=source.id
                            LEFT JOIN 	crystal_users ON task.responsible_user_id=crystal_users.id	
                            LEFT JOIN 	crystal_users   AS `crystal_users1`	ON task.user_id=crystal_users1.id
                            LEFT JOIN 	`status`  	ON	task.`status`=`status`.`id`
							LEFT JOIN asterisk_incomming ON incomming_call.uniqueid=asterisk_incomming.uniqueid
										WHERE 			task.actived=1
										 AND `task`.`date` >= '$start' and  `task`.`date` <= '$end'
										 AND `status`.call_status='$category' AND `crystal_users`.`name`='$type' 
										AND  if(ISNULL(task.incomming_call_id), category.`name`, cat.`name` )='$_REQUEST[rid]'
									
										GROUP BY task.id");
	  
$data = array(
				"aaData"	=> array()
		);

		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			$row1 = array();
			
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				$row[] = $aRow[$i];
				$a=$aRow;
				
				{
				
				}
			}
			$data['aaData'][] = $row;
		}
		echo json_encode($data); return 0;
		break;
	default :
		echo "Action Is Null!";
		break;

}



?>